
/**
 * @author zjq
 * @version 1.0
 * @date 2021/1/31 19:26
 */
public class StringUtils {
    /**
     * 根据PID拿到进程名称
     */
    public String getProcessName(String line) {
        String ss = line;
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < ss.length(); i++) {
            if (ss.charAt(i) != ' ') {
                buffer.append(ss.charAt(i));
            } else {
                break;
            }
        }
        return buffer.toString();
    }

    /**
     * 解析出PID
     */
    public String findpid(String line) {
        String ss = line;
        StringBuffer buffer = new StringBuffer();
        for (int i = 33; i >= 0; i--) {
            if (ss.charAt(i) != ' ') {
                buffer.append(ss.charAt(i));
            } else {
                break;
            }
        }
        return buffer.reverse().toString();
    }

    /**
     * 拿到IP
     */
    public String findIPAndPort(String IP) {
        String ss = IP;
        StringBuffer ip = new StringBuffer();
        for (int i = 9; i < ss.length(); i++) {
            if (ss.charAt(i) != ' ') {
                ip.append(ss.charAt(i));
            } else {
                break;
            }
        }
        return ip.toString();
    }

    /**
     * 拿到PID
     */
    public String findPID(String PID) {
        String ss = PID;
        StringBuffer pid = new StringBuffer();
        for (int i = 71; i < ss.length(); i++) {
            pid.append(ss.charAt(i));
        }
        return pid.toString();
    }

    /**
     * 拿到端口号
     */
    public String findPort(String ipAndPort) {
        String ip = ipAndPort;
        StringBuffer buffer = new StringBuffer();
        for (int i = ip.length() - 1; i >= 0; i--) {
            if (ip.charAt(i) != ':') {
                buffer.append(ip.charAt(i));
            } else {
                break;
            }
        }
        return buffer.reverse().toString();
    }
}
